package com.ruoyi.web.controller.iot;

import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.modbus.utils.CacheUtils;
import com.ruoyi.system.config.IotModbusServerConfig;
import com.ruoyi.system.domain.vo.CellLocationRequest;
import com.ruoyi.system.domain.vo.IotDeviceVo;
import com.ruoyi.system.domain.vo.IotVariableValueVo;
import com.ruoyi.system.domain.vo.OrgDevice;
import com.ruoyi.system.service.CellLocationService;
import com.ruoyi.system.service.IIotDeviceService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.web.controller.common.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import com.ruoyi.system.domain.vo.CellLocationResponse;


@RestController
@RequestMapping("/bigscreen")
@Slf4j
public class BigscreenController  extends BaseController {

    @Autowired
    private IIotDeviceService iotDeviceService;

    @Autowired
    private ISysUserService userService;

    // 设备总览
    @GetMapping(value = "/countDeviceNum")
    public AjaxResult countDeviceNum(@RequestParam(name ="deptId", required = false) Long deptId)
    {
        DeviceOverviewInfo info = new DeviceOverviewInfo();
        // 0是在线  1离线
        if(deptId != null && deptId == 100l){
            deptId = null;
        }
        List<IotDeviceVo> list = iotDeviceService.selectAllIotDeviceList(deptId);
        if(CollectionUtil.isEmpty(list)){
            info.setTotalNum(0);
            info.setOnlineNum(0);
            info.setOfflineNum(0);
        }else{
            long config =list.size();
            long  onlineconfig = list.stream().filter(a->0==a.getDeviceStatus()).count();
            long offlineconfig = list.stream().filter(a->1==a.getDeviceStatus()).count();
            info.setTotalNum(config);
            info.setOnlineNum(onlineconfig);
            info.setOfflineNum(offlineconfig);
        }
        return success(info);
    }

    // 用户总览
    @GetMapping(value = "/countUserNum")
    public AjaxResult countUserNum(@RequestParam(name ="deptId", required = false) Long deptId)
    {
        List<SysUser> list = userService.selectAllUserList();
        UserOverviewInfo userOverviewInfo = new UserOverviewInfo();
        if(CollectionUtil.isNotEmpty(list)){
            userOverviewInfo.setTotalNum(list.size());
            userOverviewInfo.setLockNum(list.stream().filter(a->"1".equals(a.getStatus())).count());
            userOverviewInfo.setNoLockNum(list.stream().filter(a->"0".equals(a.getStatus())).count());
        }
        return success(userOverviewInfo);
    }

    // 设备列表
    @GetMapping(value = "/sbtx")
    public AjaxResult sbtx(@RequestParam(name ="deptId", required = false) Long deptId)
    {
        if(deptId != null && deptId == 100l){
            deptId = null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<IotDeviceVo> list = iotDeviceService.selectAllIotDeviceList(deptId);
        List<DeviceInfo> returnList = Lists.newArrayList();
        if(CollectionUtil.isNotEmpty(list)){
            returnList = list.stream().map(a->{
                DeviceInfo deviceInfo = new DeviceInfo();
                deviceInfo.setAddr(a.getAddr());
                deviceInfo.setGatewayno(a.getDeviceName());
                deviceInfo.setOnlineState(a.getDeviceStatus());
                deviceInfo.setCreateTime(a.getCreateTime());
                deviceInfo.setCreateTimeStr(sdf.format(a.getCreateTime()));
                deviceInfo.setId(a.getId());
                deviceInfo.setDeviceSerial(a.getDeviceSerial());
                return deviceInfo;
            }).collect(Collectors.toList());
        }
        return success(returnList);
    }

    // 设备分布图  430000  岳阳
    @GetMapping(value = "/centermap")
    public AjaxResult centermap(@RequestParam("regionCode") String regionCode)
    {
        DeviceLayout deviceLayout = new DeviceLayout();
        DeviceNum deviceNum = new DeviceNum();
        if("china".equals(regionCode)){
            deviceNum.setName("湖南省");
        }else{
            deviceNum.setName("岳阳市");
        }
        deviceLayout.setRegionCode(regionCode);
        List<DeviceNum> dataList = Lists.newArrayList();
        deviceNum.setValue(1);
        dataList.add(deviceNum);
        deviceLayout.setDataList(dataList);
        return success(deviceLayout);
    }

    // 报警排名
    @GetMapping(value = "/ranking")
    public AjaxResult ranking(@RequestParam(name = "deptId", required = false) Long deptId) {
        if(deptId != null && deptId == 100l){
            deptId = null;
        }
        List<IotDeviceVo> list = iotDeviceService.selectAllIotDeviceList(deptId);
        List<DeviceInfo> returnList = Lists.newArrayList();
        if (CollectionUtil.isNotEmpty(list)) {
            returnList = list.stream().map(a -> {
                if(a.getDeviceStatus() == 0){
                    return null;
                }
                List<IotVariableValueVo> iotVariableValueVos = Lists.newArrayList();
                IotVariableValueVo iotVariableValueVo = new IotVariableValueVo();
                iotVariableValueVo.setVariableName("日流量");
                iotVariableValueVo.setVariableValue("11");
                iotVariableValueVos.add(iotVariableValueVo);
                DeviceInfo deviceInfo = new DeviceInfo();
                deviceInfo.setAddr(a.getAddr());
                deviceInfo.setGatewayno(a.getDeviceName());
                deviceInfo.setOnlineState(a.getDeviceStatus());
                deviceInfo.setCreateTime(a.getCreateTime());
                deviceInfo.setValueVoList(iotVariableValueVos);
                return deviceInfo;
            }).filter(Objects::nonNull).collect(Collectors.toList());
        }
        return success(returnList);
    }

    // 数据统计
    @GetMapping(value = "/dataStatistics")
    public AjaxResult dataStatistics(@RequestParam(name ="deptId", required = false) Long deptId) {
        if(deptId != null && deptId == 100l){
            deptId = null;
        }
        List<IotVariableValueVo> list = iotDeviceService.dataStatistics(deptId);
        return success(list);
    }

    // 机构设备数量排名
    @GetMapping(value = "/orgDeviceCount")
    public AjaxResult orgDeviceCount() {
        List<OrgDevice> list = iotDeviceService.orgDeviceCount();
        return success(list);
    }

    // 机构网管数量排名
    @GetMapping(value = "/orgGatewayCount")
    public AjaxResult orgGatewayCount(@RequestParam(name ="deptId", required = false) Long deptId) {
        List<OrgDevice> list = iotDeviceService.orgGatewayCount();
        return success(list);
    }

    @Autowired
    private IotModbusServerConfig iotModbusServerConfig;

    @GetMapping(value = "/testDingwei")
    public AjaxResult testDingwei(@RequestParam(name = "deptId", required = false) String sn) {
        log.info("执行无参方法");
        log.info("定时任务获取定位数据：sn=" + sn);
        iotModbusServerConfig.getMiiServer().sender().testIotLocation(sn, 1);
        return null;
    }

    @Autowired
    private CellLocationService cellLocationService;

    @GetMapping(value = "/testDingwei2")
    public AjaxResult testDingwei2() {
        CellLocationRequest request = new CellLocationRequest();
        request.setLac(String.valueOf(58186));
        request.setCid(String.valueOf(66328654));
        CellLocationResponse response = cellLocationService.getCellLocation(request);
        System.out.println(response);
        return null;
    }



}
